package cn.edu.scau.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.edu.scau.bean.ResultBean;
import cn.edu.scau.entity.DishCalender;
import cn.edu.scau.entity.DishCalenderShow;
import cn.edu.scau.service.DishCalenderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;



@Api(tags = "膳食统计信息控制器")
@RestController
@RequestMapping("dishcalender")
public class DishCalenderController {
	@Autowired
	private DishCalenderService dishCalenderService;
	

	/**
	  * 查询所有膳食统计信息（连接查询） ，返回DishCalenderShow对象
	 * @return
	 */
	@ApiOperation(value = "返回所有膳食统计信息")
	@GetMapping(value = "/list")
	public ResultBean list() {
		List<DishCalenderShow> dishCalenderShows = dishCalenderService.selectAll();
		ResultBean resultBean =new ResultBean(200,true,"查询所有护理级别信息成功~~~",dishCalenderShows);
		return resultBean;
	}
	

	/**
	  * 根据日期、时间段组合模糊查询,返回DishCalenderShow对象
	 * @param dishcalenderdate
	 * @param dishcalendertime
	 * @return
	 */
	@ApiOperation(value = "根据日期、时间段组合模糊查询")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(paramType = "query",name="dishcalenderdate",value = "膳食名称",dataType = "Date"),
			@ApiImplicitParam(paramType = "query",name="dishcalendertime",value = "膳食类别",dataType = "String")
	})
	@GetMapping(value = "/find")
	public ResultBean find(Date dishcalenderdate,String dishcalendertime,
			@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<DishCalenderShow> dishCalenderShows = dishCalenderService.findByDishcalenderdateDishcalendertime(new java.sql.Date(dishcalenderdate.getTime()), dishcalendertime);
		PageInfo<DishCalenderShow> pageInfo = new PageInfo<DishCalenderShow>(dishCalenderShows);
		ResultBean r =new ResultBean(200,true,"模糊查询成功~~~",pageInfo);
		return r;
	}

	/**
	 * 新增膳食统计信息
	 * @param dishCalender
	 * @param bindingResult
	 * @return
	 */
	@PostMapping(value = "/insert")
	public ResultBean insert(@Validated DishCalender dishCalender,BindingResult bindingResult)  {
		ResultBean resultBean =null;
		if(bindingResult.hasErrors()) {
			//方法一：将无法通过数据校验的信息，合并成一个字符串，返回给前端
			StringBuffer msg = new StringBuffer();
			for(FieldError f: bindingResult.getFieldErrors()) {
				msg.append(f.getField()+"："+f.getDefaultMessage()+"\n");
			}
			resultBean=new ResultBean(5005,false,msg.toString(),null);
			//方法二：将所有无法通过数据校验的信息集合，合并成一个字符串，返回给前端
//			resultBean=new ResultBean(5005,false,"实体对象赋值异常",null);
			return resultBean;
		}
		boolean flag = dishCalenderService.insert(dishCalender);
		if(flag) {
			resultBean = new ResultBean(200,true,"新增膳食统计信息成功~~~\n",null);
		}else {
			resultBean = new ResultBean(5005,false,"新增膳食统计信息失败",null);
		}
		return resultBean;
	}
	
	/**
	  * 根据膳食统计编号查询信息 
	 * @param dishcalenderid
	 * @return
	 */
	@ApiOperation(value = "根据膳食统计编号查询信息")
	@ApiImplicitParam(paramType = "path",name = "dishcalenderid",value = "编号",required = true,dataType = "int")
	@GetMapping(value = "/{dishcalenderid}")
	public ResultBean detail(@PathVariable("dishcalenderid")Integer dishcalenderid) {
		DishCalender dishCalender = dishCalenderService.selectById(dishcalenderid);
		ResultBean resultBean=new ResultBean(200,true,"查询成功~~~",dishCalender);
		return resultBean;
	}
	

	/**
	  * 修改膳食统计信息
	 * @param dishcalenderid
	 * @param dishCalender
	 * @param bindingResult
	 * @return
	 */
	@PutMapping(value = "/update/{dishcalenderid}")
	public ResultBean update(@PathVariable("dishcalenderid")Integer dishcalenderid,@Validated DishCalender dishCalender,BindingResult bindingResult) {
		ResultBean resultBean =null;
		if(bindingResult.hasErrors()) {
			//方法一：将无法通过数据校验的信息，合并成一个字符串，返回给前端
			StringBuffer msg = new StringBuffer();
			for(FieldError f: bindingResult.getFieldErrors()) {
				msg.append(f.getField()+"："+f.getDefaultMessage()+"\n");
			}
			resultBean=new ResultBean(5005,false,msg.toString(),null);
			//方法二：将所有无法通过数据校验的信息集合，合并成一个字符串，返回给前端
//			resultBean=new ResultBean(5005,false,"实体对象赋值异常",null);
			return resultBean;
		}

		boolean flag = dishCalenderService.update(dishCalender);
		if(flag) {
			resultBean = new ResultBean(200,true,"修改膳食统计信息成功~~~\n修改膳食统计信息编号为"+dishCalender.getDishcalenderid(),null);
		}else {
			resultBean = new ResultBean(5005,false,"修改膳食统计信息失败",null);
		}
		return resultBean;
	}
	
	
	/**
	  * 根据编号删除膳食信息统计信息
	 * @param dishcalenderid
	 * @return
	 */
	@DeleteMapping(value = "/delete/{dishcalenderid}")
	public ResultBean delete(@PathVariable("dishcalenderid")Integer dishcalenderid) {
		boolean flag = dishCalenderService.delete(dishcalenderid);
		ResultBean resultBean =null;
		if(flag) {
			resultBean = new ResultBean(200,true,"删除膳食统计信息成功~~~\n删除膳食统计信息编号为"+dishcalenderid,null);
		}else {
			resultBean = new ResultBean(5005,false,"删除膳食统计信息失败",null);
		}
		return resultBean;
}
}